import api from './index'

export interface FileInfo {
  id: number
  name: string
  size: number
  type: string
  uploadTime: Date
  url: string
}

export interface ShareInfo {
  id: number
  fileId: number
  shareUrl: string
  password: string
  expiry: Date
  createdAt: Date
}

// 获取文件列表
export const getFileList = () => {
  return api.get<FileInfo[]>('/files')
}

// 上传文件
export const uploadFile = (file: File) => {
  const formData = new FormData()
  formData.append('file', file)
  return api.post<FileInfo>('/files/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 下载文件
export const downloadFile = (fileId: number) => {
  return api.get(`/files/${fileId}/download`, {
    responseType: 'blob'
  })
}

// 删除文件
export const deleteFile = (fileId: number) => {
  return api.delete(`/files/${fileId}`)
}

// 分享文件
export const shareFile = (fileId: number, data: {
  expiry: string
  password?: string
}) => {
  return api.post<ShareInfo>(`/files/${fileId}/share`, data)
}

// 获取分享信息
export const getShareInfo = (shareId: string) => {
  return api.get<ShareInfo>(`/share/${shareId}`)
}

// 下载分享文件
export const downloadSharedFile = (shareId: string, password?: string) => {
  return api.get(`/share/${shareId}/download`, {
    params: { password },
    responseType: 'blob'
  })
} 